--[[
File: WoWProParse
Author: Jahwo
Last Change: xx.xx.xxxx xx:xx

Summary: Parses guides in the xml-style format
--]]




-- global Parse table
Parse = {}

-- Conditions
WoWPro.Conditions = {	get = [[get="(.-)"]], 
						turn = [[turn="(.-)"]], 
						kill = [[kill="(.-)" num="(.-)"]]}	-- has to be WoWPro. because Parse gets cleared..
				
			
			
--[[		the main parsing function		]]-- 
function WoWPro:Parse(guide)

	Parse = {}				-- clear the Parse table
	
	Parse.error = false
	
	Parse.newGuide = {}
	Parse.newGuide.Steps = {}
	
	Parse.openStep = nil	-- ID of the currently open step
	
	Parse.IDcount = 0		-- Because every step wants a different ID :)
	
	-- get guide name and id
	
	local name = string.match(guide, [[<guide="(.-)">]])		-- search the guide-tag and extract the name
	if not name then return end									-- don't parse the guide if there is no correct <guide="name"> tag
	local id = string.gsub(name, "[^%a]", "")					-- get the Guide-ID by only taking the letters in the guide name
	guide = string.gsub(guide, [[<guide="(.-)">]], "")			-- remove the guide tag
	
	Parse.newGuide.name = name
	Parse.id = id
	
	-- get all tags
	local tags = {}	
	for w in string.gmatch(guide, "<(.-)>") do
		table.insert(tags, w)
	end	
	
	for i,tag in pairs(tags) do
		----------<step>----------
		if string.match(tag,"^step") and not string.match(tag,"/$") then 
			WoWPro:ParseOpenStep()
			DEFAULT_CHAT_FRAME:AddMessage("open")
		----------</step>----------
		elseif string.match(tag,"^/step") then
			WoWPro:ParseCloseStep()
			DEFAULT_CHAT_FRAME:AddMessage("close")
		----------<step/>----------(pretty senseless, but for the sake of completeness...)
		elseif string.match(tag,"^step") and string.match(tag,"/$") then
			DEFAULT_CHAT_FRAME:AddMessage("open and close")
		----------<substep>----------
		elseif string.match(tag,"^substep") and not string.match(tag,"/$") then 
			WoWPro:ParseOpenSubStep()
			DEFAULT_CHAT_FRAME:AddMessage("open SUB")
		----------</substep>----------
		elseif string.match(tag,"^/substep") then 
			WoWPro:ParseCloseSubStep()
			DEFAULT_CHAT_FRAME:AddMessage("close SUB")
		----------<reg(/)>----------
		elseif string.match(tag,"^reg") then
			--WoWPro:ParseRegisterCondition(tag)
			DEFAULT_CHAT_FRAME:AddMessage("Condition")
		----------<coord(/)>----------
		elseif string.match(tag,"^coord") then
			DEFAULT_CHAT_FRAME:AddMessage("Coordinate")
		else
			DEFAULT_CHAT_FRAME:AddMessage("Text Added: " .. tag)
			WoWPro:ParseAddText(tag, Parse)
		end

		if Parse.error then return end
	end
	WoWPro:ParseTestOutput()
	--return Parse.id, Parse.newGuide
end



--[[		Test Output		]]--

function WoWPro:ParseTestOutput()
	for k,v in pairs(Parse.newGuide.Steps) do
		DEFAULT_CHAT_FRAME:AddMessage("["..k.."] " .. v.text)
		for x,y in pairs(v.subSteps) do
			DEFAULT_CHAT_FRAME:AddMessage("   ["..x.."] " .. y.text)
		end
	end
end



--[[		Creates a new ID		]]--

function WoWPro:ParseGetNewID()
	-- increases the IDCount and returns it
	Parse.IDcount = Parse.IDcount + 1
	return Parse.IDcount
end 



--[[		Creates a new "prototype step" and returns it		]]--

function WoWPro:ParseCreateStep()
	local newStep = {}
	-- newStep.ID = WoWPro:ParseGetNewID()    -- not needed?
 	newStep.text = ""
	newStep.conditions = {}
	return newStep
end



--[[		Adds and opens a new main-step		]]--

function WoWPro:ParseOpenStep()
	local newStep = WoWPro:ParseCreateStep()
	newStep.subSteps = {}									-- table with all substeps
	table.insert(Parse.newGuide.Steps, newStep)				-- add the new step to the guide
	Parse.openStep = newStep
end



--[[		Adds and opens a new sub-step		]]--

function WoWPro:ParseOpenSubStep()
	local newStep = WoWPro:ParseCreateStep()
	newStep.parent = Parse.openStep							-- a pointer to the parent is needed so we can "find back" to the "main"-steps
	table.insert(Parse.openStep.subSteps, newStep)
	Parse.openStep = newStep
end



--[[		Closes a main-step		]]--

function WoWPro:ParseCloseStep()
	Parse.openStep = nil	
end



--[[		Closes a sub-step		]]--

function WoWPro:ParseCloseSubStep()
	Parse.openStep = Parse.openStep.parent					-- the open is the parent of the substep.
end



--[[		Adds text to the current step		]]--

function WoWPro:ParseAddText(text)
	if Parse.openStep then
		Parse.openStep.text = Parse.openStep.text .. text .. "|n"
	end
end 



--[[		Adds a condition to the current step		]]--

function WoWPro:ParseRegisterCondition(cond)
	if Parse.openStep then											-- check if there is a open step
		for k,v in pairs(Conditions) do								-- go through all the possible conditions
			local f1,f2 = string.match(cond, v)						-- search for it in the tag
			if f1 or (f1 and f2) then								-- if one or two parameters found
				if k == "get" then 						-- get
					local newCond = {"G", f1}
				elseif k == "turn" then					-- turn
					local newCond = {"T", f1}
				elseif k == "kill" then					-- kill
					local newCond = {"K", f1, f2}
				end 
				table.insert(Parse.openStep.Conditions, newCond)	-- insert the condition to the Conditions table of the current step
			end --if f1 or (f1 and f2) then
		end --for k,v in pairs(Conditions) do
	end --if ParsecurrentLevel > 0 and Parse.currentID ~= 0 then
end



